package JackSu.java01;

import org.junit.Test;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Comparator;

public class JavaInterface implements Comparable<Double>,JavaInterfaces {

    public void interfaceTest(){
//        Arrays.sort();该方法可以对对象进行排序，但要满足该对象所属类必须实现Comparable接口
    }

    @Override
    public int compareTo(Double o) {
        System.out.println((Integer.MAX_VALUE-1)/2);//比较的值不能超过该数并且是非负整数，否则调用Integer.compare方法
        double pow = Math.pow(2, 31);
        System.out.println(pow);
        return Double.compare(1.1,1.8);
    }

    //Comparable<>和Comparator<>
    @Test
    @Override
    public void test() {
        Person zhangsan = new Person("zhangsan",28);
        Person lisi = new Person("lisi", 35);
        Person wangwu = new Person("wangwu", 54);
        Person[] persons=new Person[]{zhangsan,lisi,wangwu};
        Arrays.sort(persons);
        System.out.println(Arrays.toString(persons));
        Comparator<Person> compara =new Person();
        Arrays.sort(persons,compara);
        System.out.println(Arrays.toString(persons));

        Person[] per = persons.clone();//数组public的clone方法
        System.out.println(persons==per);
        System.out.println(Arrays.toString(per));
    }

    //按频率打印，需要实现ActionListener接口
    @Test
    @Override
    public void test02() {
        ActionListener action =new Person();
        Timer timer = new Timer(10000, action);
        timer.start();
        JOptionPane.showMessageDialog(null,"Quit program?");
        System.exit(0);
    }

    @Test
    public void testClone() throws CloneNotSupportedException {
        Person person = new Person("zhaoliu",89);
        Person clonePerson = person.clone();
        System.out.println(clonePerson==person);
        System.out.println(clonePerson);

        //默认的克隆是浅对象克隆，即子对象不会被克隆，原对象和被克隆对象的子对象共享，这是不安全的。
        // 此时需要重写克隆方法令其适用子对象克隆，如果子对象属于一个不可变的常量。如String，没有更改器会更改它，这样就是安全的。
        System.out.println(clonePerson.getName()==person.getName());
        clonePerson.setName("zhangsan");
        System.out.println(clonePerson.getName()+"-----"+person.getName());
        System.out.println(clonePerson.getName()==person.getName());

        JavaReflect reflect = new JavaReflect();
        JavaReflect javaReflect= (JavaReflect) reflect.clone();
        System.out.println(javaReflect);
    }
}
